package com.hmy.finance.service.statement.statementHandlerFactory;

import com.alibaba.fastjson.JSON;
import com.hmy.finance.api.bo.core.request.enums.StatementTypeEnum;
import com.hmy.finance.service.statement.dto.CalculateStatementDeviceDto;
import com.hmy.finance.service.statement.dto.CreateStatementDeviceDto;
import com.hmy.finance.service.statement.dto.CreateStatementDto;
import com.hmy.finance.service.statement.dto.ReconciliationStatementDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;


/**
 * 灵活周期账单拉取
 */
@Slf4j
@Component
public class FlexibleStatementHandler extends DefaultStatementHandler {
    @Override
    public ReconciliationStatementDto doStatementHandler(CreateStatementDto createStatementDto) {
        createStatementDto.getDeviceDetails().forEach(d -> {
            this.calculateStatementDeviceCycle(d);//计算该设备的当前账期的开始时间和结束时间。
        });
        return this.defaultStatementHandler(createStatementDto);
    }

    @Override
    public void calculateStatementCycle(CalculateStatementDeviceDto statementDevice) {
        log.info("calculateStatementCycle request:{}", JSON.toJSONString(statementDevice));
//        statementDevice.setCurrentRentalStartTime(statementDevice.getLastReconciliationTime());
//        if (statementDevice.getLastReconciliationTime().isEqual(statementDevice.getDeviceBeginRentTime())) {
//            //如果是首次，则去上次截止时间-即起租时间
//            statementDevice.setCurrentRentalStartTime(statementDevice.getLastReconciliationTime());
//        } else {
//            //如果不是首次，则去上次截止时间+1秒
//            statementDevice.setCurrentRentalStartTime(statementDevice.getLastReconciliationTime().plusSeconds(1));
//        }
        statementDevice.setCurrentRentalStartTime(statementDevice.getLastReconciliationTime());
        //开始时间设置为设备截止日期的月末。
        statementDevice.setCurrentRentalEndTime(calculateStatementEndTime(statementDevice.getSetPreEndTime(), statementDevice.getDeviceEndRentTime()));
        log.info("calculateStatementCycle response:{}", JSON.toJSONString(statementDevice));

    }

    @Override
    public void calculateStatementDeviceCycle(CreateStatementDeviceDto statementDevice) {
        /*灵活周期模式下d.setSetPreStartTime&d.setSetPreEndTime为前端传递，目前与其他不同，这边不需要设置*/

        log.info("calculateStatementDeviceCycle request:{}", JSON.toJSONString(statementDevice));
        statementDevice.setCurrentRentalStartTime(statementDevice.getLastReconciliationTime());
        //开始时间设置为设备截止日期的月末。
        statementDevice.setCurrentRentalEndTime(calculateStatementEndTime(statementDevice.getSetPreEndTime(), statementDevice.getDeviceEndRentTime()));
        log.info("calculateStatementDeviceCycle response:{}", JSON.toJSONString(statementDevice));
    }

    /**
     * 如果设备有退场，则对比退场时间和前端传入的时间，如果退场时间早于客户指定时间，则取退场时间，否则取客户指定时间
     *
     * @param devicePreEndTime  本次账单预结束时间(前端用户选择的结束时间)
     * @param deviceEndRentTime 设备退场时间，可能为null
     * @return LocalDateTime
     */
    public static LocalDateTime calculateStatementEndTime(LocalDateTime devicePreEndTime, LocalDateTime deviceEndRentTime) {
        LocalDateTime endTime;
        if (Objects.nonNull(deviceEndRentTime)) {
            endTime = deviceEndRentTime.isBefore(devicePreEndTime) ? deviceEndRentTime : devicePreEndTime;
        } else {
            endTime = devicePreEndTime;
        }
        return endTime;
//        return endTime.withHour(23).withMinute(59).withSecond(59).truncatedTo(java.time.temporal.ChronoUnit.SECONDS);
    }


    @Override
    public StatementTypeEnum getStatementEnumType() {
        return StatementTypeEnum.FLEXIBLE;
    }
}
